﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Collections.ObjectModel;
using System.Reflection;
using System.IdentityModel.Claims;
using System.Globalization;

namespace Veracruz.IdentityModel
{
    /// <summary>
    /// 
    /// </summary>
    public sealed class RoleClaimDefinitions
    {
        private const String RoleClaimPrefix = "http://role.veracruz.org/claims";
        private const String RoleClaimVersion = "2008/07";
        private const String RoleClaimFormat = "{0}/{1}/{2}";
        private const String RoleClaimTypeFormat = "{0}/{1}";

        #region Claims

        public static readonly string MayorRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Mayor");
        public static readonly string CitizenRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Citizen");
        public static readonly string PolicemanRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Policeman");
        public static readonly string SheriffRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Sheriff");
        public static readonly string GodfatherRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Godfather");
        public static readonly string MobsterRoleClaim = String.Format(RoleClaimDefinitions.RoleClaimFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion, "Mobster");

        public static readonly string ClaimType = String.Format(RoleClaimDefinitions.RoleClaimTypeFormat, RoleClaimDefinitions.RoleClaimPrefix, RoleClaimDefinitions.RoleClaimVersion);

        #endregion

        public static ICollection<String> GetAllRoles()
        {
            ICollection<String> __roles = new Collection<String>();
            MemberInfo[] __fieldInfo = typeof(RoleClaimDefinitions).GetMembers(BindingFlags.Public | BindingFlags.Static);
            IEnumerable<MemberInfo> __claims = __fieldInfo.Where(c => c.Name.EndsWith("Claim") && c.MemberType == MemberTypes.Field);
            return __claims.ToList().ConvertAll<string>(delegate(MemberInfo member) { return member.ReflectedType.GetField(member.Name).GetValue(null) as String; });
        }

        public static bool IsRoleClaim(Claim claim)
        {
            return String.Compare(claim.ClaimType, RoleClaimDefinitions.ClaimType, true, CultureInfo.InvariantCulture) == 0;
        }
    }
}
